package com.yohann.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yohann.eduservice.entity.EduChapter;
import com.yohann.eduservice.entity.EduCourse;
import com.yohann.eduservice.entity.EduCourseDescription;
import com.yohann.eduservice.entity.EduVideo;
import com.yohann.eduservice.entity.front.CourseVO;
import com.yohann.eduservice.entity.vo.CourseInfo;
import com.yohann.eduservice.entity.vo.CourseInfoVo;
import com.yohann.eduservice.entity.vo.CoursePublishVo;
import com.yohann.eduservice.mapper.EduCourseMapper;
import com.yohann.eduservice.service.EduChapterService;
import com.yohann.eduservice.service.EduCourseDescriptionService;
import com.yohann.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yohann.eduservice.service.EduVideoService;
import com.yohann.servicebase.exception.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Yohann
 * @since 2020-08-08
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService descriptionService;
    @Autowired
    private EduChapterService chapterService;
    @Autowired
    private EduVideoService videoService;

    /**
     * 添加课程基本信息
     * @param courseInfo
     */
    @Override
    public String saveCourseInfo(CourseInfo courseInfo) {

        //向课程表中添加课程信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfo,eduCourse);

        boolean save = this.save(eduCourse);
        if (!save){
            throw new GuliException(20001,"添加课程失败");
        }

        //向课程简介表添加简介信息
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfo.getDescription());
        courseDescription.setId(eduCourse.getId());

        descriptionService.save(courseDescription);

        return eduCourse.getId();
    }

    /**
     * 通过id获取课程信息
     * @param courseId
     * @return
     */
    @Override
    public CourseInfo getCourseInfo(String courseId) {
        //查询课程表
        EduCourse course = this.getById(courseId);

        //将课程表信息赋值到courseInfo
        CourseInfo courseInfo = new CourseInfo();
        BeanUtils.copyProperties(course,courseInfo);

        //查询描述表
        EduCourseDescription description = descriptionService.getById(courseId);
        courseInfo.setDescription(description.getDescription());

        return courseInfo;
    }

    /**
     * 修改课程信息
     * @param courseInfo
     */
    @Override
    public void updateCourseInfo(CourseInfo courseInfo) {
        //修改课程表
        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(courseInfo,course);
        boolean update = this.updateById(course);
        if (!update) {
            throw new GuliException(20001,"修改课程信息失败");
        }

        //修改描述表
        EduCourseDescription description = new EduCourseDescription();
        description.setId(courseInfo.getId());
        description.setDescription(courseInfo.getDescription());
        boolean update1 = descriptionService.updateById(description);
        if (!update1) {
            throw new GuliException(20001,"修改描述信息失败");
        }
    }

    /**
     * 通过自定义sql获取课程信息
     * @param id
     * @return
     */
    @Override
    public CoursePublishVo getCoursePublishVoById(String id) {
        return baseMapper.selectCoursePublishVoById(id);
    }

    /**
     * 通过id删除课程信息
     * @param id
     * @return
     */
    @Override
    public boolean DeleteCourse(String id) {
        //删除小节
        QueryWrapper<EduVideo> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id", id);
        videoService.removeVideo(videoQueryWrapper);

        //删除章节
        QueryWrapper<EduChapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id", id);
        chapterService.remove(chapterQueryWrapper);

        //删除描述
        descriptionService.removeById(id);

        //删除课程
        this.removeById(id);

        return true;
    }

    /**
     * 通过前端条件获取课程信息
     * @param coursePage
     * @param courseVO
     * @return
     */
    @Override
    public Map<String, Object> getCourseList(Page<EduCourse> coursePage, CourseVO courseVO) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseVO.getSubjectParentId())){
            wrapper.eq("subject_parent_id", courseVO.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseVO.getSubjectId())){
            wrapper.eq("subject_id", courseVO.getSubjectId());
        }
        if (!StringUtils.isEmpty(courseVO.getBuyCount())){
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseVO.getPrice())){
            wrapper.orderByAsc("price");
        }
        if (!StringUtils.isEmpty(courseVO.getGmtModified())){
            wrapper.orderByDesc("gmt_modified");
        }

        this.page(coursePage, wrapper);

        HashMap<String, Object> map = new HashMap<>();
        map.put("rows", coursePage.getRecords());
        map.put("current", coursePage.getCurrent());
        map.put("pages", coursePage.getPages());
        map.put("size", coursePage.getSize());
        map.put("previous", coursePage.hasPrevious());
        map.put("next", coursePage.hasNext());
        map.put("total", coursePage.getTotal());

        return map;
    }

    /**
     * 根据课程id查询课程信息
     * @param id
     * @return
     */
    @Override
    public CourseInfoVo getBaseCourseInfo(String id) {
        return baseMapper.getBaseCourseInfo(id);
    }
}
